<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'门店列表'),
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','address','type','status','tel','pcd','logo','api_sid');
	$list=pdo_getslice('jy_weishop_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['number']='NO'.sprintf("%06d", $item['id']);
				$item['pcd']=json_decode($item['pcd'],true);
				$item['url']=custom_url('/store.detail//issale=1&id='.$item['id'],true);
				$item['appurl']='jy_weishop/store.detail//issale=1&id='.$item['id'];
				$item['address']=$item['pcd']['province'].' '.$item['pcd']['city'].' '.$item['pcd']['district'].' '.$item['pcd']['street'].' '.$item['address'];
				$item['money']=pdo_getsum('jy_weishop_store_commission',array('storeid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'money');
				$item['payurl']=custom_url('jy_weishop/store.pay//sid='.$item['id'],true);
				$item['payappurl']='jy_weishop/store.pay//sid='.$item['id'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'api_sid','title'=>'门店编号','type'=>'string'),
			array('name'=>'title','title'=>'门店名称','type'=>'string'),
			array('name'=>'logo','title'=>'门店logo','type'=>'image'),
		),'action'=>array(
			array('type'=>'href','op'=>'edit','title'=>'翻译'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['htmls']=$htmls;	
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$isset=$this->pdo_get('store',array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'title'=>$post['title'],
			'diy'=>ijson_encode($post['diy'])
		);
		if($isset){
			$this->pdo_update('store',$data,array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['fromid']=$id;
			$this->pdo_insert('store',$data);
		}
		exi('设置成功');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择内容','error');	
	}
	$return['item']=$item;
	$isset=$this->pdo_get('store',array('fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['diy']=json_decode($isset['diy'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'翻译内容',
		'edit_tab'=>true,
		'edits'=>array(),
	);
	$list=array(
		array('name'=>'title','tip'=>'标题','type'=>'stringdiy'),
	);
	$is_langs=str_array($this->setting['is_langs']);
	foreach($is_langs as $en){
		$one=array();
		$one['title']=$this->langs[$en];
		$one['list']=array();
		$one['list'][]=array('type'=>'jsup','title'=>'一键翻译本页内容','action'=>'jy_lang_weishop/index/upfanyi/en='.$en,'message'=>'确定要一键翻译吗？这样将覆盖原有设置','btntitle'=>'一键翻译');
		foreach($list as $fv){
			$fv['title']=$item[$fv['name']];
			$fv['name']='diy.'.$en.'_'.$fv['name'];
			$one['list'][]=$fv;
		}
		$htmls['edits'][]=$one;
	}
	$return['item']['fanyidata']=array();
	foreach($list as $k=>$fv){
		$return['item']['fanyidata']['_'.$fv['name']]=$fv['title'];
	}
	$return['htmls']=$htmls;
	exi($return);
}